<template>
  <div class="ls-table-search-coustom">
    <!-- 表单区域 -->
    <div class="ls-table-search-form" ref="rfmediaRow">
      <!-- 表单组件 -->
      <div class="coustom-search-form" :style="{ width: formWidth }">
        <el-form
          ref="refFormRefs"
          :class="[
            'ls-form',
            {
              'is-inline': inline,
              'ls-show-all': showAll,
              'ls-show-all-border': styleType === 'border' && showAll
            },
            ''
          ]"
          inline
          :label-width="labelWidth"
          :model="model"
          v-bind="$attrs"
        >
          <el-row class="ls-search-form">
            <!-- $attrs  props 没有接收的属性 -->
            <template v-for="(item, index) in config" :key="index">
              <el-col
                v-show="index < hiddenIndex || showAll"
                v-if="!item.hidden"
                class="ls-search-col-custom"
                :class="{
                  'ls-show-col': !showAll,
                  'ls-search-col-custom-part': styleType === 'part'
                }"
              >
                <!-- 插槽 -->
                <slot v-if="item.type === 'itemSlot'" :name="item.slotName" v-bind="item"></slot>
                <!-- 二级头部 -->
                <div v-else-if="item.type === 'header'" :key="index" class="segment-title">
                  {{ item.label }}
                </div>
                <!-- 隐藏 -->
                <span
                  v-else-if="item.hide || item.hidden"
                  :key="index + 1"
                  :style="{
                    'grid-column-start': 'span 24',
                    'grid-column': 'span ' + (item.span || 24),
                    'grid-row': 'span ' + (item.row || 1),
                    display: item.hidden ? 'none' : ''
                  }"
                ></span>
                <!-- 普通的 -->
                <layout
                  v-else
                  :key="index + 2"
                  class="ls-form-item"
                  :style="{
                    'grid-column-start': 'span 24',
                    'grid-column': 'span ' + (item.span || 24),
                    'grid-row': 'span ' + (item.row || 1)
                  }"
                  :item="item"
                  :model="model"
                  :global="$attrs"
                  @mapAnewValidate="anewValidate"
                >
                  <!-- 地图走校验anewValidate -->
                  <slot
                    :name="item.slotName"
                    v-bind="{ value: model[item.prop], row: item }"
                  ></slot>
                </layout>
              </el-col>
            </template>
          </el-row>
        </el-form>
      </div>
      <!-- 展开按钮 -->
      <div class="ls-show-more">
        <el-button @click="showMore" v-if="showExpand" link style="padding: 8px 0" type="primary">
          {{ showAll ? '收起' : '展开' }}
          <el-icon
            class="ls-form-search-btn-icon"
            :style="{ transform: showAll ? 'rotate(-90deg)' : 'rotate(90deg)' }"
          >
            <DArrowRight />
          </el-icon>
        </el-button>
      </div>
    </div>

    <!-- 操作按钮区域 -->
    <div class="ls-table-btn el-form-item">
      <slot name="searchBtn"></slot>
    </div>
  </div>
</template>
<script setup>
import { DArrowRight } from '@element-plus/icons-vue'
import layout from 'ls-form/src/layout.vue'
import { ref, onMounted, onBeforeMount } from 'vue'
import { useResizeObserver } from '@vueuse/core'
const formWidth = ref('100%')
let emit = defineEmits(['update:config', 'update:model', 'collapse'])
const width = ref(0)
const props = defineProps({
  model: Object,
  config: Array,
  inline: Boolean,
  formItemWidth: {
    type: Number,
    default: 310
  },
  labelWidth: {
    type: String,
    default: 'auto'
  },
  styleType: {
    type: String,
    default: 'border' // border  part default
  }
})
const hiddenIndex = ref(3)
// 新增的搜索样式及方法
const showAll = ref(false)
const showMore = () => {
  showAll.value = !showAll.value
  emit('collapse', showAll.value)
}
// 根据自定义class获取当前是否有隐藏元素
const showExpand = ref(true)
const cloumnWidth = ref('1fr')
const getDisplayNone = (refFormWidth) => {
  // 计算form区域宽度
  const formContentWidth = refFormWidth - 100 // 80 展开按钮占位 20是内边距
  // const width=window.innerWidth
  let domListLength = document.querySelectorAll('.ls-search-col-custom').length
  // 根据表单区域宽度判断展示多少列
  const maxColumnValue = parseInt(formContentWidth / props.formItemWidth)
  const maxColumn = maxColumnValue > 0 ? maxColumnValue : 1 // 最少要容纳一项
  let formWidthValue = maxColumn * props.formItemWidth // 计算后的表单区域宽度
  formWidth.value = formWidthValue + 'px' // 加上内边距后赋值
  domListLength > maxColumn ? (showExpand.value = true) : (showExpand.value = false)
  hiddenIndex.value = maxColumn // 隐藏的表单内容起始位置/布局列数
  cloumnWidth.value = formWidthValue / maxColumn + 'px' // 网格布局宽度
}
const rfmediaRow = ref(null)
onMounted(() => {
  useResizeObserver(rfmediaRow, (entries) => {
    const entry = entries[0]
    width.value = entry.contentRect.width
    getDisplayNone(width.value)
  })
})

//结束
const refFormRefs = ref(null)
const anewValidate = (prop) => {
  refFormRefs.value.validateField(prop)
}
const validate = (callback) => {
  refFormRefs.value.validate(callback)
}
const resetFields = () => {
  refFormRefs.value.resetFields()
}
const clearValidate = (props) => {
  refFormRefs.value.clearValidate(props)
}
const setFormConfig = (config, data, prop, attr = 'options') => {
  config.find((item) => item.prop === prop)[attr] = data
  emit('update:config', config)
}
const setFormModel = (formData, data, prop) => {
  formData[prop] = data
  emit('update:model', formData)
}
const collapse = (value = false) => {
  showAll.value = value
  emit('collapse', value)
}
defineExpose({
  anewValidate,
  validate,
  resetFields,
  clearValidate,
  setFormConfig,
  setFormModel,
  collapse
})
</script>
<style lang="scss" scoped>
.ls-table-search-coustom {
  width: 100%;
  height: 52px;
  margin: 0;
  display: flex;
  .ls-table-search-form {
    flex: 1;
    height: auto;
    position: relative;
    .coustom-search-form {
      width: calc(100% - 100px);
      padding-right: 20px;
      position: relative;
      height: 100%;
      float: left;
      .ls-search-form {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(v-bind(hiddenIndex), v-bind(cloumnWidth));
        :deep(.el-select) {
          width: 100% !important;
        }

        :deep(.el-select-v2) {
          width: 100% !important;
        }

        :deep(.el-form-item) {
          margin-right: 0 !important;
          width: 100% !important;
          padding-right: 30px;
        }
        :deep(.el-form-item__label-wrap) {
          margin-left: 0 !important;
        }
        :deep(.el-date-editor.el-input) {
          width: 100% !important;
        }

        :deep(.el-input) {
          width: 100% !important;
        }
        .ls-search-col-custom-part {
          position: relative;
          &:first-child {
            &:after {
              content: '';
              width: 12px;
              height: 100%;
              background: var(--bg-page, #fff);
              position: absolute;
              z-index: 2001;
              left: -40px;
              right: 0;
            }
          }
          &:nth-child(3) {
            &:after {
              content: '';
              width: 12px;
              height: 100%;
              background: var(--bg-page, #fff);
              position: absolute;
              z-index: 2001;
              right: -30px;
              top: -2px;
            }
          }
        }
      }
      .ls-form {
        width: 100%;
        z-index: 2001;
        background: var(--bg-page, #fff);
        // border: 1px solid transparent;
        position: absolute;
        left: 0;
        top: 0;
        &.ls-show-all {
          position: absolute;
          border-radius: 0 0 8px 8px;
          box-shadow:
            2px 2px 2px var(--boxshadow-light, rgba(98, 131, 121, 0.16)),
            -2px 2px 2px var(--boxshadow-light, rgba(98, 131, 121, 0.16));
          width: calc(100% + 30px);
          left: -30px;
          padding-left: 30px;
          background: var(--bg-page, #fff);
        }
        //全border
        &.ls-show-all-border {
          position: absolute;
          border-radius: 8px;
          box-shadow: 0px 1px 10px var(--boxshadow-light, rgba(98, 131, 121, 0.16));
          width: calc(100% + 30px);
          left: -30px;
          padding-left: 30px;
          padding-top: 30px;
          top: -30px;
          background: var(--bg-page, #fff);
        }
      }
    }
    .ls-show-more {
      width: 100px;
      padding-left: 10px;
      height: 100%;
      overflow: hidden;
      float: left;
    }
  }

  .ls-table-btn {
    flex-shrink: 1;
    min-width: 370px;
    justify-content: flex-end;
  }
}
.ls-form-search-btn-icon {
  margin-left: 2px;
}
</style>
